package com.clcao.mybatis.handler;

import com.clcao.mybatis.common.constants.enums.ExceptionEnum;
import com.clcao.mybatis.common.exception.BusinessException;
import com.clcao.mybatis.common.result.Result;
import com.clcao.mybatis.common.result.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author clcao
 * @since 2025/3/11
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler{
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result<?> handleBusinessException(BusinessException e) {
        return ResultUtil.fail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<?> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ResultUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR);
    }
}
